package com.website.company.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.website.company.entity.Notice;
import com.website.company.mapper.NoticeMapper;
import com.website.company.service.NoticeService;
import com.website.company.utils.Code;
import com.website.company.utils.NoticeSearchDTO;
import com.website.company.utils.ResponseList;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author peng yangyan
 * @since 2018-11-22
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Override
    public void add(Notice product) {
        product.setCreateDatetime(new Date());
        product.setUpdateDatetime(new Date());
//        NoticeClass productClass = productClassMapper.selectById(product.getNoticeClassId());
//        product.setNoticeClassName(productClass.getName());
        baseMapper.insert(product);
    }

    @Override
    public void update(Notice product) {
        product.setUpdateDatetime(new Date());
        baseMapper.updateById(product);
    }

    @Override
    public void delete(Integer[] ids) {
        baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public Notice show(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public ResponseList<Notice> list(Integer page, Integer rows, String keywords, String productClassName) {
        EntityWrapper<Notice> entityWrapper = new EntityWrapper<>();
        entityWrapper.orderBy("paixu", true);
        int count = baseMapper.selectCount(entityWrapper);
        ResponseList<Notice> responses = new ResponseList<>();
        if (!StringUtils.isEmpty(keywords)) {
            String[] split = keywords.split("&");
            Arrays.asList(split).forEach(v -> entityWrapper.like("keywords", v));
        }
        List<Notice> news = baseMapper.selectPage(new Page<Notice>(page, rows), entityWrapper);
        responses.setRows(rows);
        responses.setPage(page);
        responses.setTotal(count);
        responses.setResult(news);
        responses.setTotalPage(count / rows + 1);
        responses.setCode(Code.SUCCESSED);
        responses.setMsg("操作成功");
        return responses;
    }

    @Override
    public ResponseList<Notice> listByOne(Integer page, Integer rows, String keywords, String productClassName) {
        EntityWrapper<Notice> entityWrapper = new EntityWrapper<>();
//        entityWrapper.eq("product_class_name", productClassName);
        entityWrapper.orderBy("paixu", true);
        int count = baseMapper.selectCount(entityWrapper);
        ResponseList<Notice> responses = new ResponseList<>();
        if (!StringUtils.isEmpty(keywords)) {
            String[] split = keywords.split("&");
            Arrays.asList(split).forEach(v -> entityWrapper.like("keywords", v));
        }
        List<Notice> news = baseMapper.selectPage(new Page<Notice>(page, rows), entityWrapper);
        responses.setRows(rows);
        responses.setPage(page);
        responses.setTotal(count);
        responses.setResult(news);
        responses.setTotalPage(count / rows + 1);
        responses.setCode(Code.SUCCESSED);
        responses.setMsg("操作成功");
        return responses;
    }

    @Override
    public ResponseList<Notice> listByFunction(Integer page, Integer rows, String keywords, Integer functionClassId) {
        ResponseList<Notice> responses = new ResponseList<>();
        NoticeSearchDTO productSearchDTO = new NoticeSearchDTO(page, rows, functionClassId, keywords);
        Integer count = baseMapper.findNoticeCount(productSearchDTO);
        productSearchDTO.setPage((page - 1) * rows);
        List<Notice> productList = baseMapper.findNoticePage(productSearchDTO);
        responses.setRows(rows);
        responses.setPage(page);
        responses.setTotal(count);
        responses.setResult(productList);
        responses.setTotalPage(count / rows + 1);
        responses.setCode(Code.SUCCESSED);
        responses.setMsg("操作成功");
        return responses;
    }

    @Override
    public List<Notice> allNotice() {
        EntityWrapper<Notice> entityWrapper = new EntityWrapper<>();
        entityWrapper.orderBy("update_datetime", false);
        return baseMapper.selectList(entityWrapper);
    }

    @Override
    public List<Notice> rssNoticeAll() {
        return baseMapper.rssNoticeAll();
    }

    @Override
    public List<Notice> getByKeyword(String keyword) {
        return baseMapper.getByKeyword(keyword);
    }


}
